/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.brain.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.thinkgem.jeesite.common.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.brain.entity.BUserRankData;
import com.thinkgem.jeesite.modules.brain.service.BUserRankDataService;

/**
 * 用户排位数据Controller
 * @author 程展峰
 * @version 2021-09-18
 */
@Controller
@RequestMapping(value = "${adminPath}/brain/bUserRankData")
public class BUserRankDataController extends BaseController {

	@Autowired
	private BUserRankDataService bUserRankDataService;
	
	@ModelAttribute
	public BUserRankData get(@RequestParam(required=false) String id) {
		BUserRankData entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bUserRankDataService.get(id);
		}
		if (entity == null){
			entity = new BUserRankData();
		}
		return entity;
	}
	
	@RequiresPermissions("brain:bUserRankData:view")
	@RequestMapping(value = {"list", ""})
	public String list(BUserRankData bUserRankData, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BUserRankData> page = bUserRankDataService.findPage(new Page<BUserRankData>(request, response), bUserRankData); 
		model.addAttribute("page", page);
		return "modules/brain/bUserRankDataList";
	}

	@RequiresPermissions("brain:bUserRankData:view")
	@RequestMapping(value = "form")
	public String form(BUserRankData bUserRankData, Model model) {
		model.addAttribute("bUserRankData", bUserRankData);
		return "modules/brain/bUserRankDataForm";
	}

	@RequiresPermissions("brain:bUserRankData:edit")
	@RequestMapping(value = "save")
	public String save(BUserRankData bUserRankData, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bUserRankData)){
			return form(bUserRankData, model);
		}
		bUserRankDataService.save(bUserRankData);
		addMessage(redirectAttributes, "保存用户排位数据成功");
		return "redirect:"+Global.getAdminPath()+"/brain/bUserRankData/?repage";
	}
	
	@RequiresPermissions("brain:bUserRankData:edit")
	@RequestMapping(value = "delete")
	public String delete(BUserRankData bUserRankData, RedirectAttributes redirectAttributes) {
		bUserRankDataService.delete(bUserRankData);
		addMessage(redirectAttributes, "删除用户排位数据成功");
		return "redirect:"+Global.getAdminPath()+"/brain/bUserRankData/?repage";
	}

	//排行榜
	//实时 不使用缓存
	//分页
	//查前100和查自己
		//剩下给前端组装

	@ResponseBody
	@RequestMapping(value = "rankHundred")
	public R rank_Hundred(BUserRankData m) {
		//?select * from   table  where rownum <101  union all   //分页
		//select * from (select *  ,rownum  from table ) t where userid = currentuser  //把当前user的排名也查出
		Page<BUserRankData> page =bUserRankDataService.findHundredRankPage(new Page(m.getPage().getPageNo(),m.getPage().getPageSize()),m);

		BUserRankData currUserRankData =bUserRankDataService.getCurrRank(m);
		return R.ok(ImmutableMap.of("page",page,"curr",currUserRankData));

	}


}